<?php
	require_once('../model/adherent.php');
	
	class DataBaseTableAdherent extends DataBaseTable {
		
		function construct() {
			parent::__construct("Adherent","Adherent");
		}

		function can_be_deleted($id) {
			$query="SELECT * FROM ".$this->table_name." WHERE ad_id=".$id;
			$result=$this->pdo->query($query);
			return ($result->rowCount()==0);
		}
		
		function relations_insert($instance)
		{
			if(!is_null($instance))
			{
				if ($instance instanceof Adherent)
				{
					
				}
			}
		}
		
		function relations_update($instance)
		{
			if(!is_null($instance))
			{
				if ($instance instanceof Adherent)
				{
					
				}
			}
		}
		
		function relations_delete($instance)
		{
			if(!is_null($instance))
			{
				if ($instance instanceof Adherent)
				{
					
				}
			}
		}
		
		function relations_select($instance)
		{
			if(!is_null($instance))
			{
				if ($instance instanceof Adherent)
				{
					
				}
			}
		}
		
		function insert_unique_i($instance)
		{
			if(!is_null($instance))
			{
				if ($instance instanceof Adherent)
				{
					$requete = $this->pdo->prepare("INSERT INTO Adherent (ad_num, ad_nom, ad_prenom, ad_num_rue, ad_lib_rue, ad_code_postal, ad_ville, ad_dte_adhesion, ad_est_actif, ad_cours_semaine, ad_level, ad_mdp, ad_mail, ad_tel) VALUES (:ad_num, :ad_nom, :ad_prenom, :ad_num_rue, :ad_lib_rue, :ad_code_postal, :ad_ville, :ad_dte_adhesion, :ad_est_actif, :ad_cours_semaine, :ad_level, :ad_mdp, :ad_mail, :ad_tel)");
					$requete->bindParam(':ad_num', $instance->getNum());
					$requete->bindParam(':ad_nom', $instance->getNom());
					$requete->bindParam(':ad_prenom', $instance->getPrenom());
					$requete->bindParam(':ad_num_rue', $instance->getNum_rue());
					$requete->bindParam(':ad_lib_rue', $instance->getLib_rue());
					$requete->bindParam(':ad_code_postal', $instance->getCode_postal());
					$requete->bindParam(':ad_ville', $instance->getVille());
					$requete->bindParam(':ad_dte_adhesion', $instance->getDte_adhesion());
					$requete->bindParam(':ad_est_actif', $instance->getEst_actif());
					$requete->bindParam(':ad_cours_semaine', $instance->getCours_semaine());
					$requete->bindParam(':ad_level', $instance->getLevel());
					$requete->bindParam(':ad_mdp', $instance->getMdp());
					$requete->bindParam(':ad_mail', $instance->getMail());
					$requete->bindParam(':ad_tel', $instance->getTel());
					
					if ($requete->execute()) // Exécute la requete et récupère l'id di la requete est valide
					{
						$query = "SELECT LAST_INSERT_ID()";
						$ad_id = $this->pdo->query($query);
						$instance->setId($ad_id);
						return $ad_id;
					}
					return -1;
				}
			}
		}
		
		// update given instance using id
		function update_unique_i($instance)
		{
			if(!is_null($instance))
			{
				if ($instance instanceof Adherent)
				{
					$requete = $this->pdo->prepare("UPDATE Adherent SET ad_num=:ad_num, ad_nom=:ad_nom, ad_prenom=:ad_prenom, ad_num_rue=:ad_num_rue, ad_lib_rue=:ad_lib_rue, ad_code_postal=:ad_code_postal, ad_ville=:ad_ville, ad_dte_adhesion=:ad_dte_adhesion, ad_est_actif=:ad_est_actif, ad_cours_semaine=:ad_cours_semaine, ad_level=:ad_level, ad_mdp=:ad_mdp, ad_mail=:ad_mail, ad_tel=:ad_tel WHERE ad_id=:ad_id");
					$requete->bindParam(':ad_num', $instance->getNum());
					$requete->bindParam(':ad_nom', $instance->getNom());
					$requete->bindParam(':ad_prenom', $instance->getPrenom());
					$requete->bindParam(':ad_num_rue', $instance->getNum_rue());
					$requete->bindParam(':ad_lib_rue', $instance->getLib_rue());
					$requete->bindParam(':ad_code_postal', $instance->getCode_postal());
					$requete->bindParam(':ad_ville', $instance->getVille());
					$requete->bindParam(':ad_dte_adhesion', $instance->getDte_adhesion());
					$requete->bindParam(':ad_est_actif', $instance->getEst_actif());
					$requete->bindParam(':ad_cours_semaine', $instance->getCours_semaine());
					$requete->bindParam(':ad_level', $instance->getLevel());
					$requete->bindParam(':ad_mdp', $instance->getMdp());
					$requete->bindParam(':ad_mail', $instance->getMail());
					$requete->bindParam(':ad_tel', $instance->getTel());
					$requete->bindParam(':ad_id', $instance->getId());
					
					return $requete->execute(); // exécution de la requête
				}
			}
		}
		// delete given instance using id
		function delete($instance)
		{
			if(!is_null($instance))
			{
				if ($instance instanceof customer)
				{
					
				}
			}
		}
		// select records and return result as array
		function select_many_t($criteria)
		{
			if(!is_null($instance))
			{
				if ($instance instanceof customer)
				{
					
				}
			}
		}
		// select records and return result as array of instances
		function select_many_i($criteria)
		{
			if(!is_null($instance))
			{
				if ($instance instanceof customer)
				{
					
				}
			}
		}
		// select records and return result as array of instances with relations
		function select_many_ir($criteria)
		{
			if(!is_null($instance))
			{
				if ($instance instanceof customer)
				{
					
				}
			}
		}
	}
?>